package com.senyin.servlet.staffInfo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.senyin.common.BaseResponse;
import com.senyin.service.StaffInfoService;
import com.senyin.service.impl.StaffInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    private StaffInfoService staffInfoService = new StaffInfoServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String captcha = req.getParameter("captcha").trim();
        String siId = req.getParameter("siId").trim();
        String siPass = req.getParameter("siPass");
        String code = (String) req.getSession().getAttribute("code");
        resp.setContentType("application/json;charset=utf-8");
        BaseResponse login = null;
        try {
            login = staffInfoService.login(siId, siPass, req,captcha,code);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        resp.getWriter().write(objectMapper.writeValueAsString(login));
    }
}
